package com.bjsxt.usercenter.common.util;

import org.apache.commons.codec.digest.DigestUtils;

public class EncrypyUtil
{
	/**
	 * 
	 * 加密
	 * @param pass 明文
	 * @return 密文   加密算法$随机数$加密(加密算法$随机数$明文)
	 */
	 
	public static String encodeStr(String pass){
		
		RegexUtil reg = new RegexUtil();
		String randStr =reg.proccedStr(32);
		String encType="sha256";
		String encStr=encType+"$"+randStr+"$";
		String encHead = encStr+pass;
		/*加密*/
		String encBack=DigestUtils.sha256Hex(encHead);
		
		return encStr+encBack;
	}
	
	/**
	 * 比较两个密码是否一致    
	 * @param souPass 明文
	 * @param encPass 密文
	 * @return 
	 */
	public static boolean checkPass(String souPass,String encPass){
		
		String[] encPassEs =encPass.split("\\$");
		String encStr =encPassEs[0]+"$"+encPassEs[1]+"$";
		String encHead =encStr+souPass;
		String encBack=DigestUtils.sha256Hex(encHead);
		encBack=encStr+encBack;
		return encBack.equals(encPass);
	}
	
	public static void main(String[] args)
	{
		EncrypyUtil en = new EncrypyUtil();
		String a=en.encodeStr("11111");
		System.out.println(a);
		boolean b =en.checkPass("11111", a);
		System.out.println(b);
	}
}
